package com.liu.springboot.study.handle;

import com.liu.springboot.study.domain.Result;
import com.liu.springboot.study.exception.StudyException;
import com.liu.springboot.study.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * 2017-01-21 13:59
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {

        logger.info("~~~~~~~~~~~~~~start handle in ExceptionHandle");
        if (e instanceof StudyException) {
            StudyException exception = (StudyException) e;
            logger.info("~~~~~~~~~~~~~~end handle in ExceptionHandle 1");
            return ResultUtil.error(exception.getCode(), exception.getMessage());
        }else {
            logger.error("【系统异常】{}", e);
            logger.info("~~~~~~~~~~~~~~end handle in ExceptionHandle 2");
            return ResultUtil.error(-1, "未知错误");
        }

    }
}
